<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/6/5
     * Time: 下午2:40
     */

    namespace app\store\admin;

    use app\admin\controller\Admin;
    use app\common\builder\ZBuilder;
    use app\common\utils\UtilTree;
    use app\store\model\CategoryModel;
    use util\Tree;

    class Category extends Admin
    {

        public function lists()
        {

            // 查询
            $map = $this->getMap();
            // 排序
            $order = $this->getOrder('id desc');

            $data_list = CategoryModel::where($map)->order($order)->select();
            Tree::config([
                'id'    => 'id',    // id名称
                'pid'   => 'pid',   // pid名称
                'title' => 'name', // 标题名称
                'child' => 'child', // 子元素键名
                'html'  => '┝ ',   // 层级标记
                'step'  => 4,       // 层级步进数量
            ]);
            $data_list = Tree::toList($data_list);
            return ZBuilder::make('table')->setPageTitle('分类管理')->setSearch(['title' => '偏好'])// 设置搜索框
            ->addColumns([ // 批量添加数据列
                           ['title_display','标题','text'],
                           ['id','ID'],
                           ['pid','上级分类','text','',CategoryModel::getColumn('id,name',[0 => '顶级分类'])],
                           ['name','分类名称','text'],
                           ['image','分类展示图','picture'],
                           ['show','状态','yesno'],
                           ['right_button','操作','btn']
            ])->addTopButtons(['add','delete'])// 批量添加顶部按钮
            ->addRightButtons(['edit','delete' => ['data-tips' => '删除后无法恢复。']])// 批量添加右侧按钮
            ->addOrder('id')->setRowList($data_list)// 设置表格数据
            ->fetch(); // 渲染模板
        }

        /**
         * @return mixed
         * @throws \think\Exception
         */
        public function add()
        {

            // 保存数据
            if ($this->request->isPost())
            {
                // 表单数据
                $data = $this->request->post();


                if ($Journey = CategoryModel::create($data))
                {

                    $this->success('新增成功','lists');
                }
                else
                {
                    $this->error('新增失败');
                }
            }
            $FleaCategoryModel = CategoryModel::getColumn('id,pid,name as title',[
                0 => [
                    'title' => '顶级分类',
                    'id'    => 0,
                    'pid'   => 0
                ]
            ]);

            $FleaCategoryModel  = UtilTree::toList($FleaCategoryModel);
            $FleaCategoryModel_ = [];
            foreach ($FleaCategoryModel as $ke => $item)
            {
                $FleaCategoryModel_[$item['id']] = $item['title_display'];
            }

            // 显示添加页面
            return ZBuilder::make('form')->setBtnTitle('back','返回前一页')->addFormItems([
                ['select','pid','上级分类','',$FleaCategoryModel_,1],
                ['text','name','分类名称'],
                ['image','image','分类图形'],
                ['text','description','分类说明'],
                ['radio','show','立即启用','',['否','是'],1]
            ])->fetch();
        }

        /**
         * @param null $id
         *
         * @return mixed
         * @throws \think\Exception
         * @throws \think\exception\DbException
         */
        public function edit($id = null)
        {

            if ($id === null)
                $this->error('缺少参数');

            // 保存数据
            if ($this->request->isPost())
            {
                // 表单数据`
                $data = $this->request->post();

                if (CategoryModel::update($data))
                {

                    $this->success('编辑成功','lists');
                }
                else
                {
                    $this->error('编辑失败');
                }
            }

            $info = CategoryModel::get($id);

            $CategoryModel = CategoryModel::getColumn('id,pid,name as title',[
                0 => [
                    'title' => '顶级分类',
                    'id'    => 0,
                    'pid'   => 0
                ]
            ]);

            $CategoryModel  = UtilTree::toList($CategoryModel);
            $CategoryModel_ = [];
            foreach ($CategoryModel as $ke => $item)
            {
                $CategoryModel_[$item['id']] = $item['title_display'];
            }
            // 显示编辑页面
            return ZBuilder::make('form')->addFormItems([
                ['hidden','id'],
                ['select','pid','上级分类','',$CategoryModel_,1],
                ['text','name','偏好名称'],
                ['image','image','分类图形'],
                ['radio','show','立即启用','',['否','是'],1]
            ])->setFormData($info)->fetch();
        }

        /**
         * 删除
         *
         * @param array $record
         *
         * @return mixed
         */
        public function delete($record = [])
        {

            return $this->setStatus('delete',[],new CategoryModel);
        }

    }